package business;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import orm.Users;
import orm.IndividualTeachingPlan;
import orm.Teacher;
import orm.TeachingPlanBaseInfo;

import dao.UserDAO;
import dao.IndividualTeachingPlanDAO;
import dao.TeacherDAO;
import dao.TeachingPlanBaseInfoDAO;
import util.Utility;
import util.DBHelper;
public class LoginAddServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public LoginAddServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String strFunc = request.getParameter("func");
		
		if (strFunc == null) {
			return;
		} else if (strFunc.equals("load")) {
			request.setAttribute("RoleID", Utility.CONSTANT_ROLEID);
			
			request.getRequestDispatcher("../" + Utility.JSP_LoginAdd).forward(request, response);
		} else if (strFunc.endsWith("save")) {
			String userName = request.getParameter("UserName");
			String pwd = request.getParameter("Pwd");
			UserDAO userDAO = new UserDAO();
			try {
				if (userDAO.doesExist(userName,pwd)) {
					request.setAttribute("flag", "flag");
					request.getRequestDispatcher("../" + Utility.JSP_LoginAdd).forward(request, response);
					return;
				}
			} catch (SQLException e) {
				
			}
			
			Users users = new Users();

//			pickupFormData(users, request);
//			insertData(users);
			
//			
//			try {
//				request.setAttribute("usersList", userDAO.getAllUserss());
//			} catch (SQLException e) {
//				
//			}
			request.getRequestDispatcher("../" + Utility.JSP_TeacherInfo).forward(request, response);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	public void pickupFormData(Teacher teacher, HttpServletRequest request) {
//		teacher.setDegree(request.getParameter(Utility.CONSTANT_DEGREE));
//		teacher.setAssignment(Utility.CONSTANT_ASSIGNMENT);
//		teacher.setLiability(Utility.CONSTANT_LIABILITY);
		
		teacher.setTeacherName(request.getParameter("TeacherName"));
		teacher.setTeacherID(Integer.parseInt(request.getParameter("TeacherID")));
		teacher.setSex(Integer.parseInt(request.getParameter("Sex")));
		String strDateTmp = request.getParameter("txtBirthdayYear")  + "-" + request.getParameter("txtBirthdayMonth") +
		"-" + request.getParameter("txtBirthdayDay");
		teacher.setBirthday(strDateTmp);
		
//		request.getSession().getAttribute("UserName").toString();
		teacher.setWorkingYears(request.getParameter("WorkingYears"));
		teacher.setDegree(Integer.parseInt(request.getParameter("Degree")));
		teacher.setPosition(request.getParameter("Position"));
		teacher.setDirection(request.getParameter("Direction"));
		teacher.setSpeciality(request.getParameter("Speciality"));
		teacher.setTeachingExperience(request.getParameter("TeachingExperience"));
		teacher.setGlory(request.getParameter("Glory"));
		teacher.setAssignment(Integer.parseInt(request.getParameter("Assignment")));
		teacher.setLiability(Integer.parseInt(request.getParameter("Liability")));
	}
	
//	public void updateData(Teacher teacher,  String teacherID) {
//		Connection conn = DBHelper.getConnection();
//		Statement stmt = null;
//		try {
//			stmt = conn.createStatement();
//			conn.setAutoCommit(false);
//			
//			TeacherDAO teacherDAO = new TeacherDAO();
//			teacher.setTeacherID(teacherID);
//			teacherDAO.updateTeacher(stmt, teacher);
//			
//			
//			conn.commit();
//			
//			stmt.close();
//			conn.close();
//		} catch (SQLException e) {
//			e.printStackTrace();
//			try {
//				conn.rollback();
//			} catch (SQLException ee) {
//				
//			}
//		}
//	}

//	public void insertData(Teacher teacher) {
//		try {
//			TeacherDAO teacherDAO = new TeacherDAO();
//			teacherDAO.insertTeacher(teacher);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//	}
	
}
